<template>
  <div>
    <el-form :model="emp" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="员工姓名" prop="empName">
        <el-input v-model="emp.empName" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="员工性别">
        <el-select v-model="emp.empSex" placeholder="请选择性别">
          <el-option label="男" value="1" ></el-option>
          <el-option label="女" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工地址">
        <el-input v-model="emp.empAddress" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="员工薪水">
        <el-input v-model="emp.empSalary" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="出生日期">
        <el-date-picker
            v-model="emp.empBirth"
            type="datetime"
            placeholder="选择日期时间" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="emp.deptId" placeholder="请选择部门">
          <el-option label="研发部" value="1"></el-option>
          <el-option label="财务部" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">更新</el-button>
        <el-button @click="cancelUpdate">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import request  from "@/network/request"
export default {
  name: 'EmpUpdate',
  watch:{
    empId:{
      immediate:true,
      handler:function (newVal,oldVal){
         this.getEmpByEmpId(newVal)
      }
    }
  },
  props:{
    empId:{
      type:Number,
      default:undefined
    }
  },
  data(){
    return {
      emp: {
        empName: '',
        empSex:null,
        empAddress: '',
        empAge: null,
        empSalary: null,
        empBirth:'',
        deptId: null,
      },
      rules: {
        empName: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 表单验证通过  发送请求执行添加操作
          request({
            url:"/emp/update",
            method:"put",
            data:this.emp
          }).then(res=>{
            // 清空表单数据
            this.resetForm(formName)
            // 提示记录添加成功
            this.$message({
              message: '记录更新成功',
              type: 'success'
            })
            // 关闭当前对话框  刷新表格数据
            this.$emit("test");
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.emp={}
    },
    getEmpByEmpId(empId){
      request({
        url:"/emp/getEmpByEmpId/"+empId,
        method:"get"
      }).then(res=>{
        this.emp= res.data
        this.emp.empSex=String(res.data.empSex)
        this.emp.deptId=String(res.data.deptId)
      })
    },
    cancelUpdate(){
      this.$emit("test");
    }
  }


}
</script>



<style scoped lang="scss">

</style>